package client;

import info.clearthought.layout.TableLayout;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Semaphore;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import utils.Utils;

import components.RPSGlassPane;
import components.RPSRoundRect;

import enums.Weapon;

/**
 * The panel that lets the players choose another weapon in case of a tie in the midst of a battle.
 */
public class WarChoosePieceComponent extends JPanel  {

	private static final long serialVersionUID = 1L;
	
	private static final int CORNER = 40;	
	private static final int PADDING = 10;
	private static final int BOARD_END_WIDTH = 570;
	private static final int BOARD_END_HEIGHT = 650;
	private static final int RECT_WIDTH = 300;
	private static final int RECT_HEIGHT = 150;
	private static final int IMAGE_SIZE = 50;
	private static final int BORDER_WIDTH = 5;
	
	private static final double SPACING_SIDE = 10;
	private static final double SPACING_UP = 10;
	
	private static final Color BACKGROUND_COLOUR = new Color (255, 255, 255, 180);
	private static final Color BACKGROUND_STROKE_COLOUR = new Color (100, 100, 100);
	
	private boolean drawBorder = false;
	
	private RPSRoundRect roundRect = new RPSRoundRect(new Dimension(BOARD_END_WIDTH, BOARD_END_HEIGHT) , RECT_WIDTH  , RECT_HEIGHT , CORNER , BACKGROUND_COLOUR , BACKGROUND_STROKE_COLOUR );
	private Rectangle startBorder = null;
	private Weapon returnedWeapon = null;

	
	public WarChoosePieceComponent () {
		setOpaque(false);
		setBounds((BOARD_END_WIDTH -RECT_WIDTH)/2 , (BOARD_END_HEIGHT -RECT_HEIGHT)/2 , RECT_WIDTH, RECT_HEIGHT);
	}
	
	
	public void createPanel(final Semaphore s, final RPSGlassPane rpsGlassPane) throws IOException {
 	
	    double [][] sizes;
	    
	    int sizeForImageWidth = (RECT_WIDTH - 2*CORNER - 2*PADDING)/3;
		double sizeForImageHeight = RECT_HEIGHT - 2*SPACING_UP - IMAGE_SIZE;
	    sizes = new double [][] {{CORNER, sizeForImageWidth ,SPACING_SIDE ,sizeForImageWidth, SPACING_SIDE, sizeForImageWidth , CORNER},
	    						 {CORNER,sizeForImageHeight , CORNER}};
	    setLayout(new TableLayout(sizes));

		MouseListener ml = new MouseListener() {
			public void mouseReleased(MouseEvent event) {}
			public void mousePressed(MouseEvent event) {}
			public void mouseExited(MouseEvent event) {
				drawBorder = false;
				repaint();
			}
			public void mouseEntered(MouseEvent event) {
				drawBorder = true;
				startBorder = ((JLabel) event.getSource()).getBounds();
				repaint();
			}
			public void mouseClicked(MouseEvent event) {
				JLabel j = (JLabel) event.getSource();
				String name = j.getName();
				int i = Integer.valueOf(name);
				Weapon weapon = Weapon.Convert(i);
				rpsGlassPane.returnedFromPanel = weapon;
				s.release();
			}
		};
		
		Weapon[] weapons = new Weapon[] {Weapon.ROCK ,Weapon.PAPER , Weapon.SCISSORS};
		JLabel[] jb = new JLabel[weapons.length];
		for (int i = 0; i < weapons.length; ++i) {
			BufferedImage img = ImageIO.read(new File("res\\"+ weapons[i].toString()+".png"));
			BufferedImage resizedImg = Utils.getScaledImage(img, IMAGE_SIZE, IMAGE_SIZE);
			jb[i] = new JLabel(new ImageIcon(resizedImg));
			jb[i].setName(String.valueOf(weapons[i].ordinal()));
			jb[i].addMouseListener(ml);			
			add(jb[i] ,String.format("%d , 1" ,1+i*2));
		}
		setVisible(true);
	}

	public void paintComponent (Graphics g) {
		super.paintComponent (g);
		
		Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        roundRect.drawRect(g2);
        
        if (drawBorder) {
        	for (int i = 0 ; i < BORDER_WIDTH ; i++) {
        		g2.drawRect(startBorder.x - i , startBorder.y - i, (int)startBorder.getWidth(), (int) startBorder.getHeight());
        	}
        }
	}

	public Weapon getSelectedWeapon() {
		return returnedWeapon;
	}
}	
